/**
* 
* ====================================================================
*
* @author apov <apov@ymail.com>
*/ 

#pragma once

#include <BaseDefine.h>
#include <map>
#include <StringObj.h>
#include <LimitInstanceMgr.h>
#include "DBLibs.h"

class DBSession;
/**
* @class DBSessionManager
*  - 
*/ 
class DBSessionManager : public LimitInstanceMgr< DBSession >{
public:
	virtual DBSession* create();
	virtual void destroy( DBSession* instance );

	int initialize( char* name, char* ip, char* db, char* id, char* passwd, size_t port, \
		char* available_check_sql, \
		size_t init_size, size_t max_size, size_t get_wait_time_tick, \
		size_t get_wait_count, size_t idle_over_remove_time, char* prepare_conn_sql, \
		char* second_name, char* api );

	DBAccount* getAccount();
	DBSessionManager();
	virtual ~DBSessionManager();

	void reset( size_t init_size, size_t max_size, size_t get_wait_time_tick, \
		size_t get_wait_count, size_t idle_over_remove_time, const char* name , char* second_name );

	bool check_dbms();
	void disable_db(){ available_dbms_ = false; }
	bool is_available_db(){ return available_dbms_; }
	const char* get_second_name();

protected:

private:
	int prepare_sql( DBSession* session );

	DBAccount account_;

	StringObj available_check_sql_;
	StringObj prepare_conn_sql_;
	StringObj second_name_;
	StringObj api_;
	
	bool available_dbms_;	

	CLock lock_;
};
